//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  MeetingInviteBoard.m
//  iHeima
//
//  Created by he chao on 5/28/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "MeetingInviteBoard.h"
#import "MeetingInviteCell.h"
#import "MeetingWaitBoard.h"

#pragma mark -

@interface MeetingInviteBoard()
{
	//<#@private var#>
}
@end

@implementation MeetingInviteBoard
DEF_SIGNAL(OK)
DEF_SIGNAL(RESET)

- (void)load
{
    arrayInviteUser = [[NSMutableArray alloc] init];
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"私董会邀请";
    self.navigationController.navigationBarHidden = NO;
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    [self showNaviBar];
    [self loadContent];
    
    

}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    if (!btnReset) {
        btnReset = [BaseButton spawn];
        btnReset.frame = CGRectMake(260, 0, 60, 44);
        [btnReset setTitle:@"重置" forState:UIControlStateNormal];
        [btnReset setTitleColor:kBlueColor forState:UIControlStateNormal];
        [btnReset addSignal:MeetingInviteBoard.RESET forControlEvents:UIControlEventTouchUpInside];
    }
    [self.navigationController.navigationBar addSubview:btnReset];
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
    [btnReset removeFromSuperview];
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(MeetingInviteBoard, signal){
    if ([signal is:MeetingInviteBoard.OK]) {
        if (arrayInviteUser.count==0) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"请选择邀请人"];
            return;
        }
        BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apichat/invitetoboard",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"bid",self.strMeetingId);
        NSMutableString *strUids = [[NSMutableString alloc] init];
        for (int i = 0; i < arrayInviteUser.count; i++) {
            [strUids appendString:arrayInviteUser[i][@"uid"]];
            if (i<arrayInviteUser.count-1) {
                [strUids appendString:@","];
            }
        }
        request.PARAM(@"uids",strUids);
        request.tag = 9528;
        [[BeeUITipsCenter sharedInstance] presentLoadingTips:@"正在邀请成员"];
//        MeetingWaitBoard *board = [[MeetingWaitBoard alloc] init];
//        board.isAdmin = YES;
//        [self.stack pushBoard:board animated:YES];
    }
    else if ([signal is:MeetingInviteBoard.RESET]) {
        for (int i = 0; i < arrayInviteUser.count; i++) {
            [arrayInviteUser[i]  setObject:@NO forKey:@"check"];
        }
        [arrayInviteUser removeAllObjects];
        [myTableView reloadData];
        [self setAvatar];
    }
}

- (void)loadContent{
    [self getFriendList];
    
    searchView = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 45)];
    searchView.strPlaceHolder = @"姓名/地名/行业/职业";
    [self.view addSubview:searchView];
    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, searchView.bottom, self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44)-50-searchView.bottom)];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
    
    imgBottom = [BeeUIImageView spawn];
    imgBottom.frame = CGRectMake(0, myTableView.bottom, self.viewWidth, 50);
    imgBottom.contentMode = UIViewContentModeScaleToFill;
    imgBottom.image = IMAGESTRING(@"invite_bottom");
    imgBottom.userInteractionEnabled = YES;
    [self.view addSubview:imgBottom];
    
    
    
    BaseButton *btnOK = [BaseButton initBaseBtn:IMAGESTRING(@"confirm_btn") highlight:IMAGESTRING(@"confirm_btn_pre")];
    btnOK.frame = CGRectMake(self.viewWidth-8-76, 8, 76, 35);
    [btnOK addSignal:MeetingInviteBoard.OK forControlEvents:UIControlEventTouchUpInside];
    [imgBottom addSubview:btnOK];
}

- (void)setAvatar{
    for (AvatarView *vi in imgBottom.subviews) {
        if ([vi isKindOfClass:[BeeUIImageView class]]) {
            [vi removeFromSuperview];
        }
        
    }
    
    for (int i = 0; i < arrayInviteUser.count; i++) {
        AvatarView *avatar = [AvatarView initFrame:CGRectMake(6+36*i, 10, 30, 30) image:nil borderColor:[UIColor clearColor]];
        [avatar setImageWithURL:[NSString stringWithFormat:@"%@avatar.php?uid=%@&size=middle",kPicUrl,arrayInviteUser[i][@"uid"]] placeholderImage:IMAGESTRING(@"default_avatar")];
        [imgBottom addSubview:avatar];
    }
}

- (void)getFriendList{
    BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apiuser/listfriends",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]);
    request.tag = 9527;
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"%@",json);
        switch ([json[@"status"] intValue]) {
            case 1:
            {
                switch (request.tag) {
                    case 9527:
                    {
                        arrayUser = json[@"res"];
                        [myTableView reloadData];
                    }
                        break;
                    case 9528:
                    {
                        MeetingWaitBoard *board = [[MeetingWaitBoard alloc] init];
                        board.isAdmin = YES;
                        board.dictMeeting = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.strMeetingId,@"id", nil];
                        [self.stack pushBoard:board animated:YES];
                    }
                        break;
                        
                    default:
                        break;
                }
                
            }
                break;
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return arrayUser.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MeetingInviteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[MeetingInviteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        //cell.selIndex = self.selIndex;
        [cell initSelf];
    }
    cell.dictUser = arrayUser[indexPath.row];
    //    cell.dictActivity = arrayActivities[indexPath.row+1];
    //    cell.backgroundColor = [UIColor clearColor];
    //    UIView *vi = [[UIView alloc] init];
    //    vi.backgroundColor = RGB(242, 242, 242);
    //    cell.backgroundView = vi;
    //
    //    UIView *viSelected = [[UIView alloc] init];
    //    viSelected.backgroundColor = [UIColor whiteColor];
    //    cell.selectedBackgroundView = viSelected;
    
    [cell load];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dictUser = arrayUser[indexPath.row];
    if ([dictUser[@"check"] boolValue]) {
        [dictUser setObject:@NO forKey:@"check"];
        [arrayInviteUser removeObject:dictUser];
    }
    else {
        if (arrayInviteUser.count>=5) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"您最多邀请5位成员"];
            return;
        }
        [dictUser setObject:@YES forKey:@"check"];
        [arrayInviteUser addObject:dictUser];
    }
    
    [myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    
    [self setAvatar];
    
    //    ActivitiesDetailBoard *board = [[ActivitiesDetailBoard alloc] init];
    //    [[MainBoard sharedInstance].stack pushBoard:board animated:YES];
    //    WebDetailBoard *board = [[WebDetailBoard alloc] init];
    //    board.type = DetailTypeActivities;
    //    board.dictActivity = arrayActivities[indexPath.row+1];
    //    [[MainBoard sharedInstance].stack pushBoard:board animated:YES];
}

@end
